import { Component, OnInit, ViewChild, Output, EventEmitter, Input } from '@angular/core';
import { InitService } from '../../public/serve/init/init.service';
import { appContentUrl } from '../content.url';
import { content } from '../content.data';
import { ContentProductTableComponent } from './table/content-product-table.component';
import { RemoveTableService } from '../../public/serve/remove-table/remove-table.service';
import { Service } from '../../server/server';
import { HintsService } from '../../public/serve/hints/hints.service';
import { CudTableService } from '../../public/serve/cud-table/cud-table.service';

declare var $:any;
@Component({
  selector: 'app-content-product',
  templateUrl: './content-product.component.html',
  styleUrls: ['./content-product.component.css']
})
export class ContentProductComponent implements OnInit {

  @ViewChild("table") table : ContentProductTableComponent;
  @Output() nodeSelect = new EventEmitter<void>();
  @Input() searchData: any;
  projectValTab : Array<object>;
  projectId:any;
  type:any = 1;

  constructor(
    public initService: InitService,
    public removeTableService: RemoveTableService,
    public request: CudTableService,
    public server: Service,
    private hintsService: HintsService
  ) { }

  isActive = this.server.isActive;
  ngOnInit() {
    this.server.eventEmit.subscribe((value: any)=>{
      this.isActive = value;
    });
  }

  /**
   * 分页
   * @param pagination 
   */ 
  _pagination(pagination: Array<any>) {
    content.pagination.pageNo = pagination[0].pageNumber;
    content.pagination.pageSize   = pagination[0].pageSize;
    content.pagination.naturalNum = (content.pagination.pageNo - 1) * content.pagination.pageSize;
    this.table.getContentProductList();
  }

  /**
   * 搜索
   */
  search(){
    content.retrieval.queryCriteria = this.searchData;
    this.table.getContentProductList();
  }

  changesearch(){
    this.search();
  }

  selectNode(val){
    content.retrieval.categoryId = val.categoryId;
    this.table.getContentProductList();
  }
  
   /**
   * 所属项目列表
   */
  getProjectList(){
    let data = this._projectTableData();
    this.request.postRequest(this.server.baseUrl + "Service/Config/getProjectList",data).then((res:any) => {
      if (res.result_code === '0000') {
        this.projectValTab = res.result;
      }
      return;
    })
    .catch((err: any)=>{
      this.hintsService.messages("error","error");
      return;
    });
  }

  //查询参数
  private _projectTableData(){
    return {
      token         : content.retrieval.token,
      pageNo        : 1,
      pageSize      : 100,
      status        : '0'
    }
  }

  /**
   * 点击所属项目
   * @param tid;
   */
  _clickProduct(pid){
    this.isActive = pid;    
    this.projectId = pid;
    content.retrieval.projectId = pid;
    this.table.getContentProductList();
  }

  public typeSearch(){
    if(this.type == 1){
      content.retrieval.projectId = '';
      this._clickProduct(content.retrieval.projectId);
    }else{
      this.getProjectList();
      content.retrieval.categoryId = '';
      this.selectNode(content.retrieval.categoryId);
    }
  }

}
